package org.freedom.fastdevpt.gos.company.service;

import java.util.List;
import java.util.Map;

import org.freedom.fastdevpt.core.base.BaseBz;
import org.freedom.fastdevpt.core.ex.DelException;
import org.freedom.fastdevpt.core.ex.SaveException;
import org.freedom.fastdevpt.core.ex.UpdateException;
import org.freedom.fastdevpt.gos.company.jdbc.CompanyMapper;
import org.freedom.fastdevpt.gos.company.pojo.Company;
import org.freedom.fastdevpt.gos.company.pojo.CompanyQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service("companyService")
public class CompanyService implements BaseBz<Integer, Company, CompanyQuery> {
	@Autowired
	private CompanyMapper companyMapper;

	@Override
	public Company getByPk(Integer pk) {

		return companyMapper.getByPk(pk);
	}

	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = { SaveException.class })
	@Override
	public void save(Company t) throws SaveException {
		try {
			if (t.getParent() == null || t.getParent().getId()==null || t.getParent().getId() == 0) {
				t.setLevel(1);
				companyMapper.save(t);

			} else {
				Company p = getByPk(t.getParent().getId());
				t.setLevel(p.getLevel() + 1);
				companyMapper.save(t);
			}

		} catch (Exception e) {
			throw new SaveException(e.getMessage());
		}
	}

	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = { SaveException.class })
	@Override
	public void batchSave(List<Company> list) throws SaveException {
		try {

			companyMapper.batchSave(list);
		} catch (Exception e) {
			throw new SaveException(e.getMessage());
		}

	}

	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = { UpdateException.class })
	@Override
	public boolean update(Company t) throws UpdateException {
		int aff = 0;
		try {
			if (t.getParent() == null || t.getParent().getId()==null || t.getParent().getId() == 0) {
				t.setLevel(1);
				companyMapper.update(t);
			} else {
				Company p = getByPk(t.getParent().getId());
				t.setLevel(p.getLevel() + 1);
				companyMapper.update(t);
			}
		} catch (Exception e) {
			throw new UpdateException(e.getMessage());
		}
		return aff == 1 ? true : false;

	}

	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = { DelException.class })
	@Override
	public boolean batchdelete(Integer[] pk) throws DelException {
		int aff = 0;
		try {

			companyMapper.batchdelete(pk);
		} catch (Exception e) {
			throw new DelException(e.getMessage());
		}
		return aff == pk.length ? true : false;

	}

	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = { DelException.class })
	@Override
	public boolean delete(Integer pk) throws DelException {
		int aff = 0;
		try {
			companyMapper.delete(pk);
		} catch (Exception e) {
			throw new DelException(e.getMessage());
		}
		return aff == 1 ? true : false;
	}

	@Override
	public List<Company> getByQuery(CompanyQuery q) {

		return companyMapper.getByQuery(q);
	}

	@Override
	public List<Map<String, Object>> getForList(CompanyQuery q) {

		return companyMapper.getForList(q);
	}

	@Override
	public Long getCountByQuery(CompanyQuery q) {

		return companyMapper.getCountByQuery(q);
	}

	public List<Company> getChildByPid(Integer pk) {
		return companyMapper.getChildByPid(pk);
	}
}
